package com.qidian.blog.servlet;

import com.qidian.blog.factory.ServiceFactory;
import com.qidian.blog.pojo.BlogCategory;
import com.qidian.blog.service.BlogCategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author 郭浩
 * @Date 2022/10/11 15:27
 */
@WebServlet("/category")
public class BlogCategoryServlet extends BaseServlet{
    private BlogCategoryService service = ServiceFactory.getService(BlogCategoryService.class);
    public void queryAll(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        respJson(service.queryAll(),resp);
    }
    public void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        int page = 1;
        int limit = 10;
        try{page = new Integer(req.getParameter("page"));}catch (NumberFormatException e){}
        try{limit = new Integer(req.getParameter("limit"));}catch (NumberFormatException e){}
        BlogCategory blogCategory = new BlogCategory();
        respJson(service.queryByPage(page,limit,blogCategory),resp);
    }
    public void get(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Long id = 0L;
        try{id = new Long(req.getParameter("categoryId"));}catch (NumberFormatException e){}
        respJson(service.queryById(id),resp);
    }
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String categoryName = req.getParameter("categoryName");
        String categoryDesc = req.getParameter("categoryDesc");
        Integer status = 1;
        try {
            status = new Integer(req.getParameter("status"));
        }catch (NumberFormatException e){}
        Long id = 1L;
        try {
            id = new Long(req.getParameter("categoryId"));
        }catch (NumberFormatException e){}
        BlogCategory category = new BlogCategory();
        category.setCategoryId(id);
        category.setStatus(status);
        category.setCategoryName(categoryName);
        category.setCategoryDesc(categoryDesc);
        respJson(service.update(category),resp);
    }
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String categoryName = req.getParameter("categoryName");
        String categoryDesc = req.getParameter("categoryDesc");
        BlogCategory category = new BlogCategory();
        category.setCategoryName(categoryName);
        category.setCategoryDesc(categoryDesc);
        respJson(service.save(category),resp);
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        Long id = 0L;
        try {
            id = new Long(req.getParameter("categoryId"));
        }catch (NumberFormatException e){}
        respJson(service.delete(id),resp);
    }
}
